package com.polaris.live.adapter.live

import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.polaris.live.common.adapter.BaseAdapter
import com.polaris.live.common.constant.CommonConst
import com.polaris.live.common.ext.autoDp
import com.polaris.live.common.ext.dp
import com.polaris.live.databinding.AdapterHomeLiveListBinding
import com.polaris.live.databinding.AdapterListBannerBinding
import com.polaris.live.resp.back_resp.DisplayUserBean
import com.polaris.live.resp.back_resp.ListWithBannerBean
import com.polaris.live.utils.image.loadWejoyImage
import com.polaris.live.widget.LevelView
import com.polaris.live.widget.refresh.BaseBindingViewHolder

/**
 * HomeLiveAdapter
 *
 * @author Created by 半仙 on 2024/2/27/027 14:20
 */
class HomeLiveAdapter : BaseAdapter<Any, BaseViewHolder>(0) {

    private var mIsHaveBanner = false

    override fun getDefItemViewType(position: Int): Int {
        return when (val item = data[position]) {
            is DisplayUserBean -> CommonConst.HomeListType.TYPE_NORMAL
            is ListWithBannerBean -> CommonConst.HomeListType.TYPE_BANNER
            else -> throw IllegalArgumentException("Unknown item type: $item")
        }
    }

    override fun onCreateDefViewHolder(
        parent: ViewGroup,
        viewType: Int,
    ): BaseBindingViewHolder<*, *> {
        val layoutInflater = LayoutInflater.from(parent.context)
        return when (viewType) {
            CommonConst.HomeListType.TYPE_BANNER -> BannerViewHolder(
                AdapterListBannerBinding.inflate(layoutInflater, parent, false), 8
            )

            CommonConst.HomeListType.TYPE_NORMAL -> NormalManViewHolder(
                AdapterHomeLiveListBinding.inflate(layoutInflater, parent, false), mIsHaveBanner
            )

            else -> throw IllegalArgumentException("Invalid view type: $viewType")
        }
    }

    override fun convert(holder: BaseViewHolder, item: Any) {
        when (holder) {
            is NormalManViewHolder -> if (item is DisplayUserBean) holder.bind(item)
            is BannerViewHolder -> if (item is ListWithBannerBean) holder.bind(item)
        }
    }

    class NormalManViewHolder(
        binding: AdapterHomeLiveListBinding,
        private val mIsHaveBanner: Boolean,
    ) : BaseBindingViewHolder<AdapterHomeLiveListBinding, DisplayUserBean>(binding) {

        override fun bind(item: DisplayUserBean) {
            mBinding.apply {
                Glide.with(root.context)
                    .loadWejoyImage(item.cover)
                    .transform(CenterCrop(), RoundedCorners(8.dp))
                    .into(userCover)

                nickName.text = item.base.nickname

                widgetState.setState(item.base, item)

                if (TextUtils.isEmpty(item.base.country)) {
                    userCountry.visibility = View.GONE
                } else {
                    userCountry.visibility = View.VISIBLE
                    userCountry.setCountry(item.base.country)
                }
                widgetSubscript.setUserSymbol(item.symbol)

                levelView.setRichCharmBySexLevel(
                    item.base.sex,
                    item.base.richs,
                    item.base.charms,
                    LevelView.LEVEL_VIEW_SMALL
                )
                vipView.setVipLevel(item.base.vipLevel, 45.autoDp, 14.autoDp)
                tvUserNum.text = item.stayUser.toString()

                val layout = consCenter.layoutParams as ViewGroup.MarginLayoutParams
                if (mIsHaveBanner) {
                    if (absoluteAdapterPosition % 2 != 0) {
                        layout.marginEnd = 9.autoDp
                    } else {
                        layout.marginEnd = 2.autoDp
                    }
                    layout.topMargin =
                        if (absoluteAdapterPosition == 1 || absoluteAdapterPosition == 2) 9.autoDp else 0.autoDp
                } else {
                    if (absoluteAdapterPosition == 0 || absoluteAdapterPosition % 2 == 0) {
                        layout.marginEnd = 9.autoDp
                    } else {
                        layout.marginEnd = 2.autoDp
                    }
                }
                consCenter.layoutParams = layout
            }
        }
    }

    fun isHaveBanner(isBanner: Boolean) {
        mIsHaveBanner = isBanner
        notifyDataSetChanged()
    }
}